package com.industrial.operation.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.OperationBenefitEvaluation;
import com.industrial.operation.domain.OperationCompanyInfo;
import com.industrial.operation.domain.PreMuJszxHy;
import com.industrial.operation.service.ICompanyHxService;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/company/hx")
public class CompanyHxController extends BaseController {

    @Autowired
    private ICompanyHxService companyHxService;

    /**
     * 查询企业行业的报表
     * @return
     */
    @GetMapping("/queryQyHyCharts")
    public AjaxResult queryQyHyCharts(OperationCompanyInfo operationCompanyInfo){
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();


        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setCounty(sysUser.getDistrict());
                operationCompanyInfo.setCity(null);
            }else{
                operationCompanyInfo.setVillage(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity(null);
            }

        }else if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setProvince(sysUser.getProvince());
            }else{
                operationCompanyInfo.setCity(operationCompanyInfo.getCity());
            }
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setCity(sysUser.getCity());
            }else{
                operationCompanyInfo.setCounty(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity(null);
            }
        }
         Map<String,Object> result = companyHxService.queryQyHyCharts(operationCompanyInfo);
        return AjaxResult.success(result);
    }

    @Autowired
    private RemoteUserService remoteUserService;

    @GetMapping("/list")
    public TableDataInfo list(OperationCompanyInfo operationCompanyInfo){

        startPage();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();


        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setCounty(sysUser.getDistrict());
                operationCompanyInfo.setCity(null);
            }else{
                operationCompanyInfo.setVillage(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity(null);
            }

        }else if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setProvince(sysUser.getProvince());
            }else{
                operationCompanyInfo.setCity(operationCompanyInfo.getCity());
            }
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(operationCompanyInfo.getCity())){
                operationCompanyInfo.setCity(sysUser.getCity());
            }else{
                operationCompanyInfo.setCounty(operationCompanyInfo.getCity());
                operationCompanyInfo.setCity(null);
            }
        }else{
            if(ArrayUtil.contains(roles.toArray(),"admin")){
                //这个分支是admin登陆
                if(StringUtils.isNotBlank(operationCompanyInfo.getCity())){
                    if(StringUtils.endsWith(operationCompanyInfo.getCity(),"0000")){
                        operationCompanyInfo.setProvince("370000");
                    }else if(StringUtils.endsWith(operationCompanyInfo.getCity(),"00")){
                        operationCompanyInfo.setProvince("370000");
                        operationCompanyInfo.setCity(operationCompanyInfo.getCity());
                    }else{
                        operationCompanyInfo.setProvince("370000");
                        String newCity = StringUtils.left(operationCompanyInfo.getCity(),4);
                        operationCompanyInfo.setCounty(operationCompanyInfo.getCity());
                        operationCompanyInfo.setCity(newCity + "00" );

                    }
                }
            }
        }

        //如果传来的市的code不是数字说明是在大屏跳进来的
        if(StringUtils.isNotBlank(operationCompanyInfo.getCity()) && !NumberUtil.isNumber(operationCompanyInfo.getCity())){
            R<List<String>> listR = remoteUserService.queryAreaCodeByAreaName(operationCompanyInfo.getCity());
            if(listR.getCode() == R.SUCCESS){
                if(listR.getData() != null && listR.getData().size() != 0){
                    List<String> data = listR.getData();
                    String s = data.get(0);
                    operationCompanyInfo.setCity(s);
                }
            }
        }

        List<OperationCompanyInfo> result = companyHxService.list(operationCompanyInfo);
        return getDataTable(result);
    }

    /**
     * 查询外层表格的年份
     * @return
     */
    @GetMapping("/queryQcTableYear")
    public AjaxResult queryQcTableYear(){
        List<String> result = companyHxService.queryQcTableYear();
        return AjaxResult.success(result);
    }

    /**
     * 加载行业数据
     * @return
     */
    @GetMapping("/loadIndustryType")
    public AjaxResult loadIndustryType(){
        List<PreMuJszxHy> result = companyHxService.loadIndustryType();
        return AjaxResult.success(result);
    }

    /**
     * 查询企业的信息
     * @param companyId
     * @return
     */
    @GetMapping("/queryCompanyInfo/{companyId}")
    public AjaxResult queryCompanyInfo(@PathVariable("companyId") String companyId) throws IOException {
        OperationCompanyInfo preMuCompanyInfo = companyHxService.queryCompanyInfo(companyId);
        return AjaxResult.success(preMuCompanyInfo);
    }

    /**
     * 查询税务数据
     * @param companyId
     * @return
     */
    @GetMapping("/queryShuiWuData/{companyId}")
    public AjaxResult queryShuiWuData(@PathVariable("companyId") String companyId){
        List<OperationBenefitEvaluation> result = companyHxService.queryShuiWuData(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询单位用地税收
     * @return
     */
    @GetMapping("/queryDwydShuiShow/{companyId}")
    public AjaxResult queryDwydShuiShow(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryDwydShuiShow(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询单位能耗销售收入
     * @return
     */
    @GetMapping("/queryDwnhXssr/{companyId}")
    public AjaxResult queryDwnhXssr(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryDwnhXssr(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询单位污染物排放销售收入
     * @return
     */
    @GetMapping("/queryDwwrwpfXssr/{companyId}")
    public AjaxResult queryDwwrwpfXssr(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryDwwrwpfXssr(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询研发经费投入强度
     * @return
     */
    @GetMapping("/queryYfjftrqd/{companyId}")
    public AjaxResult queryYfjftrqd(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryYfjftrqd(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询全员劳动生产率
     * @return
     */
    @GetMapping("/queryQyldscl/{companyId}")
    public AjaxResult queryQyldscl(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryQyldscl(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询单位用地销售收
     * @return
     */
    @GetMapping("/queryDwydxssr/{companyId}")
    public AjaxResult queryDwydxssr(@PathVariable("companyId") String companyId){
        Map<String,Object> result = companyHxService.queryDwydxssr(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询生态环境数据
     * @return
     */
    @GetMapping("/querySthjsj")
    public AjaxResult querySthjsj(OperationBenefitEvaluation operationBenefitEvaluation){
        List<Map<String, Object>> maps = companyHxService.querySthjsj(operationBenefitEvaluation);
        return AjaxResult.success(maps);
    }

    /**
     * 查询自然资源和规划数据
     * @return
     */
    @GetMapping("/queryzyzygh")
    public AjaxResult queryzyzygh(OperationBenefitEvaluation operationBenefitEvaluation){
        List<Map<String, Object>> maps = companyHxService.queryzyzygh(operationBenefitEvaluation);
        return AjaxResult.success(maps);
    }

    /**
     * 查询企业工业总产值
     * @param companyId
     * @return
     */
    @GetMapping("/queryGyzcz/{companyId}")
    public AjaxResult queryGyzcz(@PathVariable("companyId") String companyId){
        Map<String, Object> stringObjectMap = companyHxService.queryGyzcz(companyId);
        return AjaxResult.success(stringObjectMap);
    }

    /**
     * 查询企业总能耗
     * @param companyId
     * @return
     */
    @GetMapping("/queryZnh/{companyId}")
    public AjaxResult queryZnh(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryZnh(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询企业用地面积
     * @param companyId
     * @return
     */
    @GetMapping("/queryYdmj/{companyId}")
    public AjaxResult queryYdmj(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryYdmj(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询企业污染排放量
     * @param companyId
     * @return
     */
    @GetMapping("/querwrpfl/{companyId}")
    public AjaxResult querwrpfl(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.querwrpfl(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询人力资源和社会保障数据
     * @return
     */
    @GetMapping("/queryRlzyhshbz/{companyId}")
    public AjaxResult queryRlzyhshbz(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryRlzyhshbz(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询耗煤量
     * @return
     */
    @GetMapping("/queryHml/{companyId}")
    public AjaxResult queryHml(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryHml(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询耗电量
     * @return
     */
    @GetMapping("/queryHdl/{companyId}")
    public AjaxResult queryHdl(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryHdl(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询耗起量
     * @return
     */
    @GetMapping("/queryHql/{companyId}")
    public AjaxResult queryHql(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryHql(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 查询研发经费支出
     * @return
     */
    @GetMapping("/queryyfjfzc/{companyId}")
    public AjaxResult queryyfjfzc(@PathVariable("companyId") String companyId){
        Map<String, Object> result = companyHxService.queryyfjfzc(companyId);
        return AjaxResult.success(result);
    }

    /**
     * 加载该企业存在数据的年份
     * @param companyId
     * @return
     */
    @GetMapping("/loadCompanyExistDataYear/{companyId}")
    public AjaxResult loadCompanyExistDataYear(@PathVariable("companyId") String companyId){
        List<String> result = companyHxService.loadCompanyExistDataYear(companyId);
        return AjaxResult.success(result);
    }
}
